package com.example.bbs.util;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.bbs.base.Data;

@SuppressLint("StaticFieldLeak")
public final class BaseUtil {

    static final String LOG_TAG = BaseUtil.class.getSimpleName();

    private BaseUtil() throws InstantiationException {
        throw new InstantiationException("This class is not for instantiation");
    }

    // TODO(Low): 修改这里的动画效果
    public static void showToastButtonInvalid() {
        Toast.makeText(Data.context, "button invalid", Toast.LENGTH_SHORT).show();
    }

    public static void showToast(int resId) {
        showToast(Data.context.getResources().getString(resId));
    }

    public static void showToast(String str) {
        Toast.makeText(Data.context, str, Toast.LENGTH_SHORT).show();
    }

    static final int DASH_NUM = 80;

    public static void importantLog(String LogTag, String in) {
        String dashList = replicateChar('-', DASH_NUM);
        String spaceShiftToCenter = replicateChar(' ', (DASH_NUM - in.length()) / 2);
        String indent = replicateChar(' ', LogTag.length());
        Log.i(LogTag, "important log: \n" + indent + dashList + '\n' + indent + spaceShiftToCenter + in + '\n' + indent + dashList + '\n');
    }

    public static String replicateChar(char c, int n) {
        StringBuilder r = new StringBuilder();
        for (int i = 0; i < n; i++) {
            r.append(c);
        }
        return r.toString();
    }

    public static void checkActivityStack(){
        ActivityManager activityManager = (ActivityManager) Data.context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.AppTask task = activityManager.getAppTasks().get(0);
        ActivityManager.RecentTaskInfo recentTaskInfo = task.getTaskInfo();
        Log.d(LOG_TAG, "top: " + task.getTaskInfo().topActivity.getClassName() + " base: " +
                task.getTaskInfo().baseActivity.getClassName());
    }
}
